<?php

/*
 * Project: oCoder Framework
 * File: /models/user.php
 * Purpose: model for the user controller.
 * Author: Trung Hoang
 */

class UserModel extends BaseModel {

    //data passed to the user index view
    public function index() {
        // get all users
        $users = $this->find_all($table = 'account', $orderby = 'account_id DESC', $where = '', $cols = '*', $limit = '');

        $this->viewModel->set("pageTitle", "User Manager");

        $this->viewModel->set("users", $users);

        return $this->viewModel;
    }

    //create model
    public function create($data) {
        //create new item when have data
        if (!empty($data)) {
            if ($data['password'] != $data['re-password']) {
                $alert = array(
                    'class' => "alert-error",
                    'message' => "Password and re-password is not match"
                );
                $_SESSION['message'] = $alert;
                return FALSE;
            }
            //check user exist database
            else {
                unset($data['re-password']);
                $u = $this->find("account", "account_id DESC", 'username=' . $this->escape($data['username']), "*", 1);
                //debuging
                if ($u) {
                    $alert = array(
                        'class' => "alert-error",
                        'message' => "Username is exist!"
                    );
                    $_SESSION['message'] = $alert;
                    return FALSE;
                } else {


                    //return id of item after insert to db
                    return $this->save($table = 'account', $data = $data, $where = '');
                }
            }
        }

        //param var for view
        $this->viewModel->set("pageTitle", "Create User");
        return $this->viewModel;
    }

    //update model
    public function edit($id, $data) {
        //update item when have data
        if (!empty($data)) {
            //return bool

            return $this->save($table = 'account', $data = $data, $where = 'account_id=' . $id);
        }

        //get item detail by id
        $user = $this->find($table = 'account', $orderby = 'account_id DESC', $where = 'account_id=' . $id, $cols = '*', $limit = '');

        //param var for view
        $this->viewModel->set("pageTitle", "Update User");
        $this->viewModel->set("user", $user);
        return $this->viewModel;
    }

}

?>